package com.sangengblog.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.sangengblog.constants.SystemConstants;
import com.sangengblog.domain.ResponseResult;
import com.sangengblog.domain.dto.ChangeStatusDto;
import com.sangengblog.domain.entity.Role;
import com.sangengblog.domain.vo.PageVo;
import com.sangengblog.enums.AppHttpCodeEnum;
import com.sangengblog.mapper.RoleMapper;
import com.sangengblog.service.RoleService;
import kotlin.jvm.internal.Lambda;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import java.util.ArrayList;
import java.util.List;

/**
 * 角色信息表(Role)表服务实现类
 *
 * @author makejava
 * @since 2024-04-10 22:04:19
 */
@Service("roleService")
public class RoleServiceImpl extends ServiceImpl<RoleMapper, Role> implements RoleService {


    @Override
    public List<String> selectRoleKeyByUserId(Long id) {
        // 判断是否是管理员 如果是返回集合中只需要有admin
        if (id == 1L || id == 2L) {
            ArrayList<String> roleKeys = new ArrayList();
            roleKeys.add("admin");
            return roleKeys;
        }
        return getBaseMapper().selectRoleKeyByUserId(id);
    }

    @Override
    public ResponseResult list(int pageNum, int pageSize, String roleName, String status) {
        LambdaQueryWrapper<Role> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.like(StringUtils.hasText(roleName), Role::getRoleName, roleName);
        queryWrapper.like(StringUtils.hasText(status), Role::getStatus, status);
        queryWrapper.orderByAsc(Role::getRoleSort);
        Page page = new Page(pageNum, pageSize);
        page(page, queryWrapper);
        // 转化为RoleVo

        PageVo pageVo = new PageVo(page.getRecords(), page.getTotal());

        return ResponseResult.okResult(pageVo);
    }

    @Override
    public ResponseResult changeStatus(ChangeStatusDto changeStatusDto) {
        LambdaUpdateWrapper<Role> updateWrapper = new LambdaUpdateWrapper<>();
        updateWrapper.eq(Role::getId, changeStatusDto.getRoleId());
        updateWrapper.set(Role::getStatus, changeStatusDto.getStatus());
//        Role role = getOne(updateWrapper);
        update(updateWrapper);
        return ResponseResult.okResult();
    }

    @Override
    @Transactional
    public ResponseResult addRole(Role role) {
//        save(role);
//        Long roleId = getBaseMapper().getRole(role).getId();
//        for(Long menuId : role.getMenuIds()) {
//            getBaseMapper().insertRoleMenu(roleId, menuId);
//        }
        try {
            save(role);
            Long roleId = getBaseMapper().getRole(role).getId();
            for(Long menuId : role.getMenuIds()) {
                getBaseMapper().insertRoleMenu(roleId, menuId);
            }
            return ResponseResult.okResult();
        } catch (Exception e) {
            return ResponseResult.errorResult(AppHttpCodeEnum.SYSTEM_ERROR, e.getMessage());
        }
    }

    @Override
    public ResponseResult getRoleInfo(Long id) {
        LambdaQueryWrapper<Role> wrapper = new LambdaQueryWrapper<Role>().eq(Role::getId, id);
        Role role = getOne(wrapper);
        return ResponseResult.okResult(role);
    }

    @Override
    @Transactional
    public ResponseResult updateRole(Role role) {
        LambdaUpdateWrapper<Role> updateWrapper = new LambdaUpdateWrapper<Role>().eq(Role::getId, role.getId());
        update(role, updateWrapper);
        // 删除之前关联的菜单
        getBaseMapper().deleteRoleMenuByRoleId(role.getId());
        // 重新关联
        for(Long menuId : role.getMenuIds()) {
            getBaseMapper().insertRoleMenu(role.getId(), menuId);
        }
        return ResponseResult.okResult();
    }

    @Override
    public ResponseResult deleteRole(Long id) {
        LambdaUpdateWrapper<Role> updateWrapper = new LambdaUpdateWrapper<>();
        updateWrapper.eq(Role::getId, id);
        updateWrapper.set(Role::getDelFlag, SystemConstants.DEL_FLAG);
        update(updateWrapper);
        return ResponseResult.okResult();
    }

    @Override
    public ResponseResult listAllRole() {
        LambdaQueryWrapper<Role> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Role::getStatus, SystemConstants.STATUS_NORMAL);
        List<Role> roles = list(queryWrapper);
        return ResponseResult.okResult(roles);
    }
}
